package com.cj.mobile.iabp.mvp.ui.activity;

import static com.jess.arms.utils.Preconditions.checkNotNull;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.KeyboardUtils;
import com.cj.mobile.iabp.R;
import com.cj.mobile.iabp.app.global.Constant;
import com.cj.mobile.iabp.app.utils.CommonUtils;
import com.cj.mobile.iabp.app.utils.DecimalInputUtils;
import com.cj.mobile.iabp.di.component.DaggerAddRemoveConcernInfoComponent;
import com.cj.mobile.iabp.mvp.contract.AddRemoveConcernInfoContract;
import com.cj.mobile.iabp.mvp.model.entity.RemoveInfo;
import com.cj.mobile.iabp.mvp.presenter.AddRemoveConcernInfoPresenter;
import com.cj.mobile.iabp.mvp.ui.adapter.SignInImgListAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created on 2025/09/30 18:56
 * 新增撤机关注信息
 *
 * @author Love_xie
 * module name is AddRemoveConcernInfoActivity
 */
public class AddRemoveConcernInfoActivity extends BaseActivity<AddRemoveConcernInfoPresenter> implements AddRemoveConcernInfoContract.View {
    /*----------------------------------------------------------------控件信息----------------------------------------------------------------*/
    @BindView(R.id.txvi_addremoveconcerninfo_code)
    TextView txviCode;                                                                              // 登记号
    @BindView(R.id.txvi_addremoveconcerninfo_name)
    TextView txviName;                                                                              // 患者姓名
    @BindView(R.id.group_addremoveconcerninfo_concern)
    RadioGroup rgConcern;                                                                           // 关注
    @BindView(R.id.txvi_addremoveconcerninfo_removetime)
    TextView txviRemoveTime;                                                                        // 撤机时间
    @BindView(R.id.imvi_addremoveconcerninfo_removetime)
    ImageView imviRemoveTime;

    @BindView(R.id.cb_addremoveconcerninfo_azc_pwl)
    CheckBox cbAzcPwl;                                                                              // 安装侧
    @BindView(R.id.cb_addremoveconcerninfo_azc_pwn)
    CheckBox cbAzcPwn;
    @BindView(R.id.cb_addremoveconcerninfo_azc_zbkcj)
    CheckBox cbAzcZbkcj;
    @BindView(R.id.cb_addremoveconcerninfo_azc_zbbkcj)
    CheckBox cbAzcZbbkcj;
    @BindView(R.id.cb_addremoveconcerninfo_azc_xzfh)
    CheckBox cbAzcXzfh;
    @BindView(R.id.cb_addremoveconcerninfo_azc_wjfh)
    CheckBox cbAzcWjfh;
    @BindView(R.id.cb_addremoveconcerninfo_azc_crtdy)
    CheckBox cbAzcCrtdy;
    @BindView(R.id.cb_addremoveconcerninfo_azc_crtxy)
    CheckBox cbAzcCrtxy;


    @BindView(R.id.cb_addremoveconcerninfo_dc_pwl)
    CheckBox cbDcPwl;                                                                               // 对侧
    @BindView(R.id.cb_addremoveconcerninfo_dc_pwn)
    CheckBox cbDcPwn;
    @BindView(R.id.cb_addremoveconcerninfo_dc_zbkcj)
    CheckBox cbDcZbkcj;
    @BindView(R.id.cb_addremoveconcerninfo_dc_zbbkcj)
    CheckBox cbDcZbbkcj;
    @BindView(R.id.cb_addremoveconcerninfo_dc_xzfh)
    CheckBox cbDcXzfh;
    @BindView(R.id.cb_addremoveconcerninfo_dc_wjfh)
    CheckBox cbDcWjfh;
    @BindView(R.id.cb_addremoveconcerninfo_dc_crtdy)
    CheckBox cbDcCrtdy;
    @BindView(R.id.cb_addremoveconcerninfo_dc_crtxy)
    CheckBox cbDcCrtxy;

    @BindView(R.id.lila_addremoveconcerninfo_img)
    LinearLayout lilaImg;
    @BindView(R.id.revi_addremoveconcerninfo_img)
    RecyclerView reviImgList;                                                                       // 特殊或异常情况拍照


    @BindView(R.id.edit_addremoveconcerninfo_ast)
    EditText editAst;                                                                               // AST
    @BindView(R.id.edit_addremoveconcerninfo_alt)
    EditText editAlt;                                                                               // ALT
    @BindView(R.id.edit_addremoveconcerninfo_cr)
    EditText editCr;                                                                                // 肌酐
    @BindView(R.id.edit_addremoveconcerninfo_egfr)
    EditText editEgfr;                                                                              // eGFR
    @BindView(R.id.edit_addremoveconcerninfo_myo)
    EditText editMyo;                                                                               // 肌红蛋白
    @BindView(R.id.edit_addremoveconcerninfo_clac)
    EditText editClac;                                                                              // 乳酸cLac
    @BindView(R.id.edit_addremoveconcerninfo_myog)
    EditText editMyog;                                                                              // 肌钙蛋白
    @BindView(R.id.edit_addremoveconcerninfo_zsszmqnj)
    EditText editZsszmqnj;                                                                          // 左室舒张末期内径
    @BindView(R.id.edit_addremoveconcerninfo_zssxfs)
    EditText editZssxfs;                                                                            // 左室射血分数
    @BindView(R.id.edit_addremoveconcerninfo_nirsl)
    EditText editNirsL;                                                                             // NIRS左
    @BindView(R.id.edit_addremoveconcerninfo_nirsr)
    EditText editNirsR;                                                                             // NIRS右

    /*----------------------------------------------------------------业务信息----------------------------------------------------------------*/
    // 对话框
    private MaterialDialog mDialog;
    // 图片适配器
    @Inject
    SignInImgListAdapter mImgAdapter;
    @Inject
    List<String> mRecyImgList;
    @Inject
    GridLayoutManager mGridLayoutManager;

    @Override
    protected void onDestroy() {
        KeyboardUtils.unregisterSoftInputChangedListener(getWindow());
        DefaultAdapter.releaseAllHolder(reviImgList);
        if (mDialog != null) {
            this.mDialog.dismiss();
        }
        super.onDestroy();

        this.mDialog = null;
        this.mImgAdapter = null;
        this.mGridLayoutManager = null;
        this.mRecyImgList = null;
    }

    /**
     * 根据主题使用不同的颜色。
     * 如果想要纯透明，则需要重写此方法，返回值为 -1 即可。
     */
    public int useStatusBarColor() {
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(android.R.color.white, typedValue, true);
        return typedValue.data;
    }

    /**
     * 将状态栏改为浅色模式(状态栏 icon 和字体会变成深色)
     */
    public boolean useLightStatusBar() {
        return true;
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerAddRemoveConcernInfoComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_add_remove_concern_info;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setTitle("撤机关注信息");

        // 初始化Loading对话框
        mDialog = new MaterialDialog.Builder(this).content(R.string.common_execute).progress(true, 0).build();

        initRecyclerView();

        // 创建输入框数组
        EditText[] decimalInputs = {editAst, editAlt, editCr, editEgfr, editMyo, editClac, editMyog, editZsszmqnj, editZssxfs, editNirsL, editNirsR};
        // 批量设置，限制小数点后2位
        DecimalInputUtils.setupDecimalInputs(decimalInputs, 2);

        if (mPresenter != null) {
            mPresenter.initData(getIntent().getExtras());
        }
    }

    private void initRecyclerView() {
        ArmsUtils.configRecyclerView(reviImgList, mGridLayoutManager);
        reviImgList.setAdapter(mImgAdapter);
        mImgAdapter.setOnItemClickListener((view, viewType, data, position) -> {
            String val = (String) data;
            showImage(val);
        });
    }

    /**
     * 显示图片
     */
    public void showImage(String image) {
        if (!TextUtils.isEmpty(image)) {
            Intent intent = new Intent(this, PhotoViewActivity.class);
            intent.putExtra(Constant.IMAGE_URL, image);
            startActivity(intent);
        }
    }

    @OnClick({
            R.id.btn_addremoveconcerninfo_save,                                                     // 暂存
            R.id.btn_addremoveconcerninfo_submit,                                                   // 提交
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_addremoveconcerninfo_save:                                                // 暂存
                if (!CommonUtils.isDoubleClick()) {
                    onSubmit("0");
                }
                break;
            case R.id.btn_addremoveconcerninfo_submit:                                              // 提交
                if (!CommonUtils.isDoubleClick()) {
                    onSubmit("1");
                }
                break;
        }
    }

    private void onSubmit(String pre) {
        if (mPresenter != null) {
            mPresenter.updateRemove(pre,
                    isConcern(),
                    editAst.getText().toString(),
                    editAlt.getText().toString(),
                    editCr.getText().toString(),
                    editEgfr.getText().toString(),
                    editMyo.getText().toString(),
                    editClac.getText().toString(),
                    editMyog.getText().toString(),
                    editZsszmqnj.getText().toString(),
                    editZssxfs.getText().toString(),
                    editNirsL.getText().toString(),
                    editNirsR.getText().toString(),
                    getLessL(), getLessR()
            );
        }
    }

    /**
     * 是否关注
     */
    private String isConcern() {
        if (rgConcern.getCheckedRadioButtonId() != -1) {
            if (rgConcern.getCheckedRadioButtonId() == R.id.radio_addremoveconcerninfo_concern_yes) {
                return "是";
            } else {
                return "否";
            }
        }

        return "";
    }


    private List<String> getLessL() {
        List<String> lessL = new ArrayList<>();
        if (cbAzcPwl.isChecked()) {
            lessL.add("皮温凉");
        }
        if (cbAzcPwn.isChecked()) {
            lessL.add("皮温暖");
        }
        if (cbAzcZbkcj.isChecked()) {
            lessL.add("足背可触及");
        }
        if (cbAzcZbbkcj.isChecked()) {
            lessL.add("足背不可触及");
        }
        if (cbAzcXzfh.isChecked()) {
            lessL.add("下肢发花");
        }
        if (cbAzcWjfh.isChecked()) {
            lessL.add("未见发花");
        }
        if (cbAzcCrtdy.isChecked()) {
            lessL.add("CRT>3s");
        }
        if (cbAzcCrtxy.isChecked()) {
            lessL.add("CRT<3s");
        }
        return lessL;
    }

    private List<String> getLessR() {
        List<String> lessR = new ArrayList<>();
        if (cbDcPwl.isChecked()) {
            lessR.add("皮温凉");
        }
        if (cbDcPwn.isChecked()) {
            lessR.add("皮温暖");
        }
        if (cbDcZbkcj.isChecked()) {
            lessR.add("足背可触及");
        }
        if (cbDcZbbkcj.isChecked()) {
            lessR.add("足背不可触及");
        }
        if (cbDcXzfh.isChecked()) {
            lessR.add("下肢发花");
        }
        if (cbDcWjfh.isChecked()) {
            lessR.add("未见发花");
        }
        if (cbDcCrtdy.isChecked()) {
            lessR.add("CRT>3s");
        }
        if (cbDcCrtxy.isChecked()) {
            lessR.add("CRT<3s");
        }
        return lessR;
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void showRemoveInfo(String code, String name, RemoveInfo info) {
        txviCode.setText(CommonUtils.isEmptyReturnStr(code, "暂无"));
        txviName.setText(CommonUtils.isEmptyReturnStr(name));
        if (info != null) {
            // 撤机时间
            if (!TextUtils.isEmpty(info.getRemoveTime())) {
                txviRemoveTime.setText(info.getRemoveTime());
//                imviRemoveTime.setVisibility(View.VISIBLE);
            }

            // 关注
            if (!TextUtils.isEmpty(info.getFocuse())) {
                if (info.getFocuse().equals("是")) {
                    ((RadioButton) findViewById(R.id.radio_addremoveconcerninfo_concern_yes)).setChecked(true);
                } else {
                    ((RadioButton) findViewById(R.id.radio_addremoveconcerninfo_concern_not)).setChecked(true);
                }
            }


            if (!TextUtils.isEmpty(info.getLessL())) {
                String[] lessL = info.getLessL().split(",");
                for (int i = 0; i < lessL.length; i++) {
                    switch (lessL[i]) {
                        case "皮温凉":
                            cbAzcPwl.setChecked(true);
                            break;
                        case "皮温暖":
                            cbAzcPwn.setChecked(true);
                            break;
                        case "足背可触及":
                            cbAzcZbkcj.setChecked(true);
                            break;
                        case "足背不可触及":
                            cbAzcZbbkcj.setChecked(true);
                            break;
                        case "下肢发花":
                            cbAzcXzfh.setChecked(true);
                            break;
                        case "未见发花":
                            cbAzcWjfh.setChecked(true);
                            break;
                        case "CRT>3s":
                            cbAzcCrtdy.setChecked(true);
                            break;
                        case "CRT<3s":
                            cbAzcCrtxy.setChecked(true);
                            break;
                    }
                }
            }


            if (!TextUtils.isEmpty(info.getLessR())) {
                String[] lessR = info.getLessR().split(",");
                for (int i = 0; i < lessR.length; i++) {
                    switch (lessR[i]) {
                        case "皮温凉":
                            cbDcPwl.setChecked(true);
                            break;
                        case "皮温暖":
                            cbDcPwn.setChecked(true);
                            break;
                        case "足背可触及":
                            cbDcZbkcj.setChecked(true);
                            break;
                        case "足背不可触及":
                            cbDcZbbkcj.setChecked(true);
                            break;
                        case "下肢发花":
                            cbDcXzfh.setChecked(true);
                            break;
                        case "未见发花":
                            cbDcWjfh.setChecked(true);
                            break;
                        case "CRT>3s":
                            cbDcCrtdy.setChecked(true);
                            break;
                        case "CRT<3s":
                            cbDcCrtxy.setChecked(true);
                            break;
                    }
                }
            }

            if (!TextUtils.isEmpty(info.getImages())) {
                String[] path = info.getImages().split(",");

                // 接收当前选中的图
                mRecyImgList.clear();
                Collections.addAll(mRecyImgList, path);
                // 接下来展示图片
                mImgAdapter.notifyDataSetChanged();

                lilaImg.setVisibility(View.VISIBLE);
            } else {
                lilaImg.setVisibility(View.GONE);
            }

            editAst.setText(CommonUtils.isEmptyReturnStr(info.getAst()));
            editAlt.setText(CommonUtils.isEmptyReturnStr(info.getAlt()));
            editCr.setText(CommonUtils.isEmptyReturnStr(info.getCr()));
            editEgfr.setText(CommonUtils.isEmptyReturnStr(info.getEgfr()));
            editMyo.setText(CommonUtils.isEmptyReturnStr(info.getMyo()));
            editClac.setText(CommonUtils.isEmptyReturnStr(info.getClac()));
            editMyog.setText(CommonUtils.isEmptyReturnStr(info.getGa()));
            editZsszmqnj.setText(CommonUtils.isEmptyReturnStr(info.getLeftSize()));
            editZssxfs.setText(CommonUtils.isEmptyReturnStr(info.getLeftScore()));
            editNirsL.setText(CommonUtils.isEmptyReturnStr(info.getNirsLeft()));
            editNirsR.setText(CommonUtils.isEmptyReturnStr(info.getNirsRight()));
        }
    }

    @Override
    public void showLoadingSubmit() {
        if (mDialog != null)
            mDialog.show();
    }

    @Override
    public void hideLoadingSubmit() {
        if (mDialog != null)
            mDialog.hide();
    }

}